/*
 * Copyright 2024 The CHANGLEI Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flatterer/src/indicate_border.dart';
import 'package:flatterer/src/popup_window.dart';
import 'package:flatterer/src/widget_group.dart';
import 'package:flutter/material.dart';

/// 构建下拉列表item
typedef DropdownMenuItemBuilder = List<Widget> Function(BuildContext context);

const _constraints = BoxConstraints(
  minWidth: 150,
  maxWidth: 300,
  maxHeight: 300,
);

/// Created by changlei on 2020/10/24.
///
/// 下拉框
class DropdownMenu extends StatelessWidget {
  /// 更多弹框
  const DropdownMenu({
    Key? key,
    required this.items,
    this.divider = const Divider(
      indent: 40,
      endIndent: 10,
    ),
    this.constraints = _constraints,
  }) : super(key: key);

  /// item
  final List<Widget> items;

  /// 分割线
  final Widget divider;

  /// 约束
  final BoxConstraints constraints;

  @override
  Widget build(BuildContext context) {
    final animation = ModalRoute.of(context)?.animation;
    Widget child = SingleChildScrollView(
      child: WidgetGroup(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        direction: Axis.vertical,
        divider: divider,
        children: items,
      ),
    );
    if (animation != null) {
      child = SizeTransition(
        axisAlignment: -1,
        sizeFactor: CurvedAnimation(
          parent: animation,
          curve: const Interval(0.2, 0.8),
          reverseCurve: const Interval(0.2, 0.8),
        ),
        child: child,
      );
    }
    return Container(
      constraints: constraints,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
      ),
      clipBehavior: Clip.antiAlias,
      child: IntrinsicWidth(
        child: child,
      ),
    );
  }
}

/// 显示DropDownMenu
Future<T?> showDropdownMenu<T>({
  required BuildContext context,
  required List<Widget> items,
  Widget divider = const Divider(
    indent: 40,
    endIndent: 10,
  ),
  Axis direction = Axis.vertical,
  RSize indicateSize = const RSize(
    24,
    12,
    radius: 2.08,
    blRadius: 9.6,
    brRadius: 9.6,
  ),
  double offset = -10,
  EdgeInsetsGeometry margin = const EdgeInsets.all(10),
  bool preferBelow = true,
  BoxConstraints constraints = _constraints,
  bool useSafeArea = false,
}) async {
  assert(items.isNotEmpty == true);
  return showPopupWindow<T>(
    context,
    indicateSize: indicateSize,
    offset: offset,
    margin: margin,
    preferBelow: preferBelow,
    direction: direction,
    useSafeArea: useSafeArea,
    builder: (context) {
      return DropdownMenu(
        items: items,
        divider: divider,
        constraints: constraints,
      );
    },
  );
}
